"""Authentication middleware.

The authentication middleware provides a technique for Django to automatically
provide a ``user`` attribute on the request that dynamically maps to the
current user.
"""

class LazyUser(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_user'):
            from hcp.auth.core import get_user
            request._cached_user = get_user(request)
        return request._cached_user

class AuthenticationMiddleware(object):
    def process_request(self, request):
        assert hasattr(request, 'session')
        request.__class__.user = LazyUser()
        return None
